package io.renren.utils.util.db.connection;

import java.util.Hashtable;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.log4j.Logger;

import io.renren.utils.util.SystemConfigUtil;


/**
 * @author Clark Liu
 *
 */
public class LocalDataSourceFactory implements DataSourceFactory {

	static Logger logger = Logger.getLogger(LocalDataSourceFactory.class.getName());
	public final static String PREFACE = "DBConnection";
	
	private String defaultDS = null;
    private Hashtable datasources = new Hashtable<String, DataSource>();
	
	//private String datasource;
	
	private static Hashtable<String, Properties> jdbcConfigs = new Hashtable<String, Properties>();

	static {
		loadJDBCConfig();		
	}
	
	 private static void loadJDBCConfig() {		 
		
		 logger.info("loadJDBCConfig...");
				String poolName = null;
				String aa = PREFACE + "." + "CONNECTION.NAME";
				String bb = SystemConfigUtil.getInstance().getStringProperty(aa);
				for (int i = 0; (poolName = SystemConfigUtil.getInstance().getStringProperty(
						PREFACE + "." + "CONNECTION[" + i + "].NAME", null)) != null; i++) {
					try {
						String driver = SystemConfigUtil.getInstance().getStringProperty(PREFACE + ".CONNECTION[" + i + "].DRIVER");
						String url = SystemConfigUtil.getInstance().getStringProperty(PREFACE + ".CONNECTION[" + i + "].URL");
						String user = SystemConfigUtil.getInstance().getStringProperty(PREFACE + ".CONNECTION[" + i + "].USER");
						String password = SystemConfigUtil.getInstance().getStringProperty(
								PREFACE + ".CONNECTION[" + i + "].PASSWORD");
						int maxConnections = SystemConfigUtil.getInstance().getIntProperty(
								PREFACE + ".CONNECTION[" + i + "].MAXACTIVE", 20);
						Properties configProperties = new Properties();
						configProperties.put(CONFIGPREFIX, poolName);
						configProperties.put(CONFIGDRIVERCLASSNAME, driver);
						configProperties.put(CONFIGURL, url);
						configProperties.put(CONFIGUSERNAME, user);
						configProperties.put(CONFIGPASSWORD, password);
						configProperties.put(CONFIGDBCPMAXACTIVE, maxConnections);
						//if (logger.isEnabledFor(Level.DEBUG)) {
							logger.info("Adding datasource : " + poolName);
						//}
						jdbcConfigs.put(poolName, configProperties);
					} catch (Exception e) {
						logger.error("Can't loading jdbc properties ",e);
					}
				}
				logger.info("loadJDBCConfig done.");
	    }
	 

	public LocalDataSourceFactory() {

	}

	public  DataSource getDataSource(String datasourceName) {
		DataSource ds = (DataSource)datasources.get(datasourceName);
		
		if (ds == null) {
			Properties configProperties = jdbcConfigs.get(datasourceName) ;
			if (configProperties!=null) {
				//return new SDataSource(sourceName);
				ds = new PoolingDataSource(configProperties);
				datasources.put(datasourceName, ds);
			} else {
				throw new RuntimeException ("No datasource "+datasourceName+" was defined in "+JDBC_CONFIG_FILE+".");
			}
		}
		
		return ds;
	}

	@Override
	public String getDefaultDS() {
		return defaultDS;
	}

	@Override
	public void setDefaultDS(String defaultDS) {
		this.defaultDS = defaultDS;		
	}

}
